#include "bsp_config.h"

BEEP_QUEUE_ENUM_Typedef beep_queue_enum;
BEEP_QUEUE_ENUM_Typedef *pBEEP_QUEUE_ENUM;

/**
 *  @brief 蜂鸣器PWM初始化
 *  @param 无
 *  @brief 无  
 */
void Beep_Config(void)
{
    /*
     * Prepare and set configuration of timers
     * that will be used by LED Controller
     */
    ledc_timer_config_t ledc_timer = {
        .duty_resolution = LEDC_TIMER_9_BIT, // resolution of PWM duty
        .freq_hz = 2700,                      // frequency of PWM signal
        .speed_mode = LEDC_LOW_SPEED_MODE,    // timer mode
        .timer_num = LEDC_TIMER_1,            // timer index
        .clk_cfg = LEDC_AUTO_CLK,             // Auto select the source clock
    };
    // Set configuration of timer0 for high speed channels
    ledc_timer_config(&ledc_timer);

    /*
     * Prepare individual configuration
     * for each channel of LED Controller
     * by selecting:
     * - controller's channel number
     * - output duty cycle, set initially to 0
     * - GPIO number where LED is connected to
     * - speed mode, either high or low
     * - timer servicing selected channel
     *   Note: if different channels use one timer,
     *         then frequency and bit_num of these channels
     *         will be the same
     */
    ledc_channel_config_t ledc_channel = {
        .channel = LEDC_CHANNEL_0,
         .duty = 0,
         .gpio_num = BEEP_GPIO,
         .speed_mode = LEDC_LOW_SPEED_MODE,
         .hpoint = 0,
         .timer_sel = LEDC_TIMER_1,
    };

    // Set LED Controller with previously prepared configuration
    ledc_channel_config(&ledc_channel);

    // Initialize fade service.
    ledc_fade_func_install(0);
}

/**
 *  @brief 开启蜂鸣器
 *  @param 无
 *  @brief 无  
 */
static void Beep_On(void)
{
    ledc_set_duty(LEDC_LOW_SPEED_MODE,LEDC_CHANNEL_0, 256);
    ledc_update_duty(LEDC_LOW_SPEED_MODE,LEDC_CHANNEL_0);
}


/**
 *  @brief 关闭蜂鸣器
 *  @param 无
 *  @brief 无  
 */
static void Beep_Off(void)
{
    ledc_set_duty(LEDC_LOW_SPEED_MODE,LEDC_CHANNEL_0, 0);
    ledc_update_duty(LEDC_LOW_SPEED_MODE,LEDC_CHANNEL_0);
}

/**
 *  @brief 蜂鸣器滴一声
 *  @param 无
 *  @brief 无  
 */
static void Beep_Once(void)
{
    Beep_On();
    vTaskDelay(100/portTICK_PERIOD_MS);
    Beep_Off();
}

/**
 *  @brief 蜂鸣器滴三声
 *  @param 无
 *  @brief 无  
 */
static void Beep_Three(void)
{
    Beep_On();
    vTaskDelay(60/portTICK_PERIOD_MS);
    Beep_Off();
    vTaskDelay(15/portTICK_PERIOD_MS);

    Beep_On();
    vTaskDelay(60/portTICK_PERIOD_MS);
    Beep_Off();
    vTaskDelay(15/portTICK_PERIOD_MS);

    Beep_On();
    vTaskDelay(60/portTICK_PERIOD_MS);
    Beep_Off();
}

/**
 *  @brief 蜂鸣器500ms
 *  @param 无
 *  @brief 无  
 */
static void Beep_500ms(void)
{
    Beep_On();
    vTaskDelay(500/portTICK_PERIOD_MS);
    Beep_Off();
}

/**
 *  @brief 蜂鸣器 驱动线程
 *  @param 无
 *  @brief 无  
 */
void Beep_Driver_Task(void *param)
{
    while (1)
    {
        xQueueReceive(beep_QueueHandler, &pBEEP_QUEUE_ENUM, portMAX_DELAY);
        switch (*pBEEP_QUEUE_ENUM)
        {
            case BEEP_ONCE:Beep_Once();break;
            case BEEP_THREE:Beep_Three();break;
            case BEEP_500MS:Beep_500ms();break;
            default: break;
        }
    }
    
}

